package study.mq.rocketmq.producer.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import study.eggs.result.Result;
import study.eggs.result.Results;
import study.mq.rocketmq.producer.support.utils.ResultUtils;

@Slf4j
@RestController
public class _2_AsyncController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/async")
    public Result async(@RequestBody Request request) {

        rocketMQTemplate.asyncSend(request.getDest(), request.getData(), new SendCallback() {
            @Override
            public void onSuccess(SendResult result) {
                log.info(">>>>>>>>>> {}", result.getSendStatus());
            }

            @Override
            public void onException(Throwable ex) {
                log.error("", ex);
            }
        });
        return Results.ok();
    }
}
